package vg.services.user_interface_manager.additional_swing_components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class Locker {
	// Defines
	public static final int DEF_LOCK = 1;
	public static final int DEF_UNLOCK = 0;
	// Components
	private final JPanel view;
	private final JButton locker;
	private final ImageIcon lockIcon, unlockIcon;
	private final JLabel label;
	// Data
	private int station = DEF_UNLOCK;
	// Mutex
	private final Object theMutexObject;
	/**
	 * Constructor.
	 */
	public Locker(String title) {
		this.theMutexObject = new Object();
		this.view = new JPanel(new GridBagLayout());
		this.lockIcon = new ImageIcon("./data/resources/textures/lock.png");
		this.unlockIcon = new ImageIcon("./data/resources/textures/unlock.png");
		this.label = new JLabel(title);
		Border titled = BorderFactory.createTitledBorder(title);
		this.view.setBorder(titled);

		this.locker = new JButton(this.unlockIcon);
		this.locker.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (theMutexObject) {
					innerSetStation();
				}
			}
		});
		// package of interface
		GridBagConstraints gbc = new GridBagConstraints(0,0, 1,1, 0,0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0,0);
		this.view.add(this.locker, gbc);
		
		gbc = new GridBagConstraints(1,0, 1,1, 1,1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 5, 0, 0), 0,0);
		this.view.add(this.label, gbc);
	}
	
	public int getStation() {
		synchronized (theMutexObject) {			
			return station;
		}
	}
	/**
	 * This method sets new station for locker.
	 * Use <b>DEF_LOCK</b> and <b>DEF_UNLOCK</b>.
	 */
	public void setStation(int newStat) {
		synchronized (theMutexObject) {
			innerSetStation(newStat);
		}
	}
	/**
	 * This method changes station.
	 */
	public void changeStation() {
		synchronized (theMutexObject) {
			innerSetStation();
		}
	}
	
	public void addActionListener(ActionListener l) {
		locker.addActionListener(l);
	}
	
	public void removeListener(ActionListener l) {
		locker.removeActionListener(l);
	}
	
	public JPanel getView() {
		return view;
	}
//=============================================================================
//-------------PRIVATE METHODS-------------------------------------------------
	private void innerSetStation(int newStat) {
		station = newStat;
		switch(station) {
			case DEF_LOCK:
			{
				locker.setIcon(lockIcon);
				break;
			}
			case DEF_UNLOCK:
			{
				locker.setIcon(unlockIcon);
				break;
			}
		}
		view.updateUI();
	}
	
	private void innerSetStation() {
		switch(station) {
			case DEF_LOCK:
			{
				station = DEF_UNLOCK;
				locker.setIcon(unlockIcon);
				break;
			}
			case DEF_UNLOCK:
			{
				station = DEF_LOCK;
				locker.setIcon(lockIcon);
				break;
			}
		}
		view.updateUI();
	}
}
